package GUILayer;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.*;

import CtrLayer.CtrEmployee;

import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

/**
 * @author Danalachi Sergiu
 */
public class MainW 
{
	
	private CtrEmployee ctrEmployee;

	public MainW()
	{
		ctrEmployee = new CtrEmployee();
		
		initComponents();
	}

	public static void main(String[] args) {
		new MainW();
	}
	
	
	
	
	private JFrame loginFrame;
	private JLabel usernameLabel;
	private JPanel logoPanel;
	private JLabel passwordLabel;
	private JButton button1;
	private JTextField username;
	private JPasswordField passwordField1;
	private JLabel loginMessage;
	
	private void initComponents() 
	{
		final String invalidLoginMessage = "Invalid Username Or Password";
		
		loginFrame = new JFrame();
		usernameLabel = new JLabel();
		logoPanel = new JPanel();
		passwordLabel = new JLabel();
		button1 = new JButton();
		username = new JTextField();
		passwordField1 = new JPasswordField();

		// ======== loginFrame ========
		{
			loginFrame.setTitle("ApartmentBox Property Management System \u00a9 Group 1 - 2014");
			try{loginFrame.setIconImage(new ImageIcon(getClass().getResource("/icons/logoSign.png")).getImage());}
			catch(Exception ex) {System.err.println("Logo Image Not Found 1");}
			loginFrame.setVisible(true);
			loginFrame.setResizable(false);
			loginFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			loginFrame.setForeground(Color.black);
			loginFrame.setBackground(new Color(237, 237, 237));
			Container loginFrameContentPane = loginFrame.getContentPane();

			// ---- usernameLabel ----
			usernameLabel.setText("Username");
			usernameLabel.setFont(new Font("Times New Roman", Font.PLAIN, 24));

			// ======== logoPanel ========
			logoPanel.setLayout(new FlowLayout());
			try {
				logoPanel.add(new JLabel(new ImageIcon((getClass().getResource("/icons/logo.png")))));
			} catch (Exception e) {System.err.println("Logo Image Not Found 2");}

			// ---- passwordLabel ----
			passwordLabel.setText("Password");
			passwordLabel.setFont(new Font("Times New Roman", Font.PLAIN, 24));

			// ---- button1 ----
			button1.setText("Log In");
			button1.setForeground(new Color(9, 141, 9));
			button1.setFont(new Font("Verdana", Font.BOLD, 25));
			button1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String user = username.getText();
					String pass = new String(passwordField1.getPassword());
					
					final int pid = ctrEmployee.login(user, pass);
					
					if(pid != -1)
					{
						loginFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
						
						// open main program frame
						Thread mainWindow = new Thread(new Runnable() {
							public void run() {
								new MainWindow(pid);
							}
						});
						
						mainWindow.start();
						
						
						try {
							mainWindow.join();
						} catch (InterruptedException e1) {
							e1.printStackTrace();
						}
						
						//destroy login frame
						loginFrame.setVisible(false);	
						loginFrame.dispose();
					}
					else
					{
						loginMessage.setText(invalidLoginMessage);
					}
				}
			});

			// ---- username ----
			username.setFont(new Font("Tw Cen MT", Font.BOLD, 26));
			username.setText("username");
			username.setForeground(new Color(9, 141, 9));
			username.addFocusListener(new FocusListener() {
				public void focusLost(FocusEvent e) {}
				public void focusGained(FocusEvent e) {
					username.setText("");
					loginMessage.setText("");
				}
			});
			username.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					button1.doClick();
				}
			});
			
			
			
			// ---- passwordField1 ----
			passwordField1.setText("password");
			passwordField1.setForeground(new Color(9, 141, 9));
			passwordField1.setFont(new Font("Tahoma", Font.PLAIN, 20));
			passwordField1.setBackground(Color.white);
			passwordField1.addFocusListener(new FocusListener() {
				public void focusLost(FocusEvent e) {}
				public void focusGained(FocusEvent e) {
					passwordField1.setText("");
					loginMessage.setText("");
				}
			});
			
			passwordField1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					button1.doClick();
				}
			});
			
			
			loginMessage = new JLabel();
			loginMessage.setForeground(Color.RED);
			loginMessage.setHorizontalAlignment(SwingConstants.RIGHT);
			loginMessage.setFont(new Font("Verdana", Font.BOLD, 14));
			
			
			GroupLayout loginFrameContentPaneLayout = new GroupLayout(loginFrameContentPane);
			loginFrameContentPaneLayout.setHorizontalGroup(
				loginFrameContentPaneLayout.createParallelGroup(Alignment.LEADING)
					.addGroup(loginFrameContentPaneLayout.createSequentialGroup()
						.addGap(69)
						.addGroup(loginFrameContentPaneLayout.createParallelGroup(Alignment.TRAILING)
							.addComponent(logoPanel, GroupLayout.PREFERRED_SIZE, 456, GroupLayout.PREFERRED_SIZE)
							.addGroup(loginFrameContentPaneLayout.createSequentialGroup()
								.addGroup(loginFrameContentPaneLayout.createParallelGroup(Alignment.TRAILING)
									.addComponent(username, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 199, GroupLayout.PREFERRED_SIZE)
									.addComponent(usernameLabel, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE))
								.addGap(18, 70, Short.MAX_VALUE)
								.addGroup(loginFrameContentPaneLayout.createParallelGroup(Alignment.LEADING)
									.addGroup(loginFrameContentPaneLayout.createSequentialGroup()
										.addComponent(passwordLabel, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.RELATED, 77, Short.MAX_VALUE))
									.addComponent(passwordField1, GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)))
							.addGroup(loginFrameContentPaneLayout.createSequentialGroup()
								.addComponent(loginMessage, GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(button1, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)))
						.addContainerGap(64, Short.MAX_VALUE))
			);
			loginFrameContentPaneLayout.setVerticalGroup(
				loginFrameContentPaneLayout.createParallelGroup(Alignment.TRAILING)
					.addGroup(loginFrameContentPaneLayout.createSequentialGroup()
						.addContainerGap()
						.addComponent(logoPanel, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addGroup(loginFrameContentPaneLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(usernameLabel, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
							.addComponent(passwordLabel, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
						.addGap(19)
						.addGroup(loginFrameContentPaneLayout.createParallelGroup(Alignment.LEADING, false)
							.addComponent(passwordField1)
							.addComponent(username, GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE))
						.addGroup(loginFrameContentPaneLayout.createParallelGroup(Alignment.TRAILING)
							.addGroup(loginFrameContentPaneLayout.createSequentialGroup()
								.addGap(28)
								.addComponent(button1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addGroup(loginFrameContentPaneLayout.createSequentialGroup()
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(loginMessage, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)))
						.addGap(43))
			);
			loginFrameContentPane.setLayout(loginFrameContentPaneLayout);
			loginFrame.pack();
			loginFrame.setLocationRelativeTo(null);
		}
	}
}
